'use strict';

const db = uniCloud.database()

exports.main = async (event, context) => {
	const { page = 1, pageSize = 100, where = {} } = event
	
	try {
		// 计算跳过的数量
		const skip = (page - 1) * pageSize
		
		// 查询构建
		let query = db.collection('food_categories')
		
		// 应用筛选条件
		if (Object.keys(where).length > 0) {
			query = query.where(where)
		}
		
		// 获取分类数据
		const { data } = await query.skip(skip).limit(pageSize).get()
		
		return {
			code: 0,
			data: data,
			total: data.length
		}
	} catch (e) {
		return {
			code: -1,
			message: e.message
		}
	}
}; 